import java.sql.*;

public class WorldSql3 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String jdbcUrl="jdbc:mysql://localhost:3308/world";
        String user="root";
        String password="";
        try(Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
            Statement statement = conn.createStatement()) {
            ResultSet result = statement.executeQuery("SELECT Name FROM country WHERE LifeExpectancy=" +
                            "(SELECT MAX(LifeExpectancy) FROM country) UNION SELECT Name FROM country " +
                            "WHERE LifeExpectancy=(SELECT MIN(LifeExpectancy) FROM country)");
            result.next();
            System.out.print("世界上的平均寿命最长的国家:");
            System.out.printf("%s\n",result.getString(1));
            result.next();
            System.out.print("世界上的平均寿命最短的国家:");
            System.out.printf("%s\n",result.getString(1));
        } catch(SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
}